package algorithms.que14;

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;

/**
 * @功能
 * @创建时间 2020/9/8
 */
public class E1d4d18 {
    public static int min(int[] a)
    {
        int lo=1;
        int hi=a.length-2;
        int mid;
        int localMinIndex=-1;
        //find in rang smaller
        while(lo<=hi && localMinIndex==-1)
        {
            mid=(lo+hi)/2;
            if(a[mid]<a[mid-1] && a[mid]<a[mid+1])
                localMinIndex=mid;
            else if(a[mid-1]<a[mid+1])
                hi=mid-1;
            else if(a[mid-1]>a[mid+1])
                lo=mid+1;
        }
        //
        lo=1;
        hi=a.length-2;
        while(lo<=hi && localMinIndex==-1)
        {
            mid=(lo+hi)/2;
            if(a[mid]<a[mid-1] && a[mid]<a[mid+1])
                localMinIndex=mid;
            else if(a[mid-1]<a[mid+1])
                lo=mid+1;
            else if(a[mid-1]>a[mid+1])
                hi=mid-1;
        }
        return localMinIndex;
    }//end min
    public static void main(String[] args)
    {
        int[] a= In.readInts(args[0]);
        StdOut.println(min(a));
    }
}
